//
var vue_sms_login = new Vue({
    el: '#sms_login_app',
    data: {
        // 短信登录表单
        smsForm: {
            phone: '',
            code: ''
        },
        // 发送验证码相关
        sendCodeDisabled: false,
        countdown: 0,
        sendCodeText: '发送验证码',
        countdownTimer: null,
        redirectUrl: '/dashboard' // 默认重定向到dashboard
    },
    created: function () {
        // 从URL参数中获取重定向地址
        const urlParams = new URLSearchParams(window.location.search);
        const redirect = urlParams.get('redirect');
        if (redirect) {
            this.redirectUrl = decodeURIComponent(redirect);
        }
    },
    methods: {
        // 发送短信验证码
        sendSmsCode: function () {
            if (!this.smsForm.phone) {
                return this.$message.error('请输入手机号！');
            }
            
            if (!this.isValidPhone(this.smsForm.phone)) {
                return this.$message.error('请输入正确的手机号格式！');
            }

            this.sendCodeDisabled = true;
            this.sendCodeText = '发送中...';

            // 调用发送验证码API
            axios.post('/sms/send-code', {
                phone: this.smsForm.phone
            }).then(function (response) {
                var res = response.data;
                if (res.code === 200) {
                    this.$message.success('验证码发送成功，请查收！');
                    this.startCountdown();
                } else {
                    this.$message.error('发送失败：' + (res.message || '请稍后重试'));
                    this.resetSendCodeButton();
                }
            }.bind(this)).catch(function (error) {
                this.$message.error('请求失败：' + error.message);
                this.resetSendCodeButton();
            }.bind(this));
        },

        // 短信验证码登录
        handleSmsLogin: function () {
            if (!this.smsForm.phone) {
                return this.$message.error('请输入手机号！');
            }
            
            if (!this.smsForm.code) {
                return this.$message.error('请输入验证码！');
            }

            axios.post('/sms/login', {
                phone: this.smsForm.phone,
                code: this.smsForm.code
            }).then(function (response) {
                var res = response.data;
                if (res.code === 200) {
                    var rd = res.data || {}
                    window.localStorage.setItem('yumoAiAdmin:userInfo', JSON.stringify(rd));
                    this.$message.success('登录成功！');
                    setTimeout(function () {
                        window.location.href = this.redirectUrl;
                    }.bind(this), 1000);
                } else {
                    this.$message.error('登录失败：' + (res.message || '请稍后再试！'));
                }
            }.bind(this)).catch(function (error) {
                this.$message.error('请求失败：' + error.message);
            }.bind(this));
        },

        // 开始倒计时
        startCountdown: function () {
            this.countdown = 60;
            this.countdownTimer = setInterval(function () {
                this.sendCodeText = this.countdown + 's后重新发送';
                this.countdown--;
                
                if (this.countdown < 0) {
                    this.resetSendCodeButton();
                    clearInterval(this.countdownTimer);
                }
            }.bind(this), 1000);
        },

        // 重置发送验证码按钮
        resetSendCodeButton: function () {
            this.sendCodeDisabled = false;
            this.sendCodeText = '发送验证码';
        },

        // 手机号格式验证
        isValidPhone: function (phone) {
            return /^1[3-9]\d{9}$/.test(phone);
        },

        // 格式化手机号输入（只允许数字）
        formatPhone: function (event) {
            var value = event.target.value.replace(/\D/g, '');
            this.smsForm.phone = value;
        },

        // 格式化验证码输入（只允许数字）
        formatCode: function (event) {
            var value = event.target.value.replace(/\D/g, '');
            this.smsForm.code = value;
        }
    },

    // 组件销毁时清理定时器
    beforeDestroy: function () {
        if (this.countdownTimer) {
            clearInterval(this.countdownTimer);
        }
    }
})
